//
//  UINavigationBar+setBackgroundImage.m
//  LvZhongMagicKey
//
//  Created by LvZhongFaBao_Mac on 2018/8/8.
//  Copyright © 2018年 LZFB. All rights reserved.
//

#import "UINavigationBar+setBackgroundImage.h"

@implementation UINavigationBar (setBackgroundImage)

- (void)setClearImage {
    
    [self setTitleTextAttributes:@{NSForegroundColorAttributeName:KWhiteColor,
                                     NSFontAttributeName:SYSTEMFONT(18)}];
    
    [self setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [self setShadowImage:[UIImage new]];
}

- (void)setDefaultImage {
    
    [self setTitleTextAttributes:@{NSForegroundColorAttributeName:CFontTitleBlackColor,
                                     NSFontAttributeName:SYSTEMFONT(18)}];
    
    UIColor *color = CNavBgMainColor;
    UIImage *bgImage = [[self class] imageWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64) color:color];
    [self setBackgroundImage:bgImage forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    
    UIImage *shadowImage = [[self class] imageWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0.5) color:CLineColor];
    [self setShadowImage:shadowImage];
}

+ (UIImage *)imageWithFrame:(CGRect)frame color:(UIColor *)color {
    frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    
    UIGraphicsBeginImageContext(frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, frame);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

@end
